import { Channels } from '/imports/models/Channels';

Template.channel_panel_created_channels.onCreated(function () {
  this.limit = new ReactiveVar(15);
  this.hasMore = new ReactiveVar(false);

  this.autorun(() => {
    let lmt = this.limit.get();
    this.resultsSub = this.subscribe('get.created.channels', {}, lmt);
    this.total = Counts.get('get.created.channels.counts');
    if (this.total > lmt) {
      this.hasMore.set(true);
    } else {
      this.hasMore.set(false);
    }
  });
});

Template.channel_panel_created_channels.helpers({
  results: function () {
    return Channels.find({
      'owner.id': Meteor.userId()
    }, {
      limit: Template.instance().limit.get(),
      sort: {
        createdAt: -1
      }
    });
  },

  hasMore: function () {
    return Template.instance().hasMore.get();
  }
});

Template.channel_panel_created_channels.events({
  'click .channel-panel-load-more' (e, instance) {
    e.preventDefault();
    let limit = instance.limit.get();
    if (instance.total > limit) {
      instance.limit.set(limit + 15);
    }
  }
});